part of YFree;

///YFree启动函数
void yRunApp({required YPage launchPage, MaterialColor? primarySwatch, required Function() onYConfig, Function(BuildContext context, Widget? child)? builder, ThemeData? theme}) {
  onYConfig();
  FlutterError.onError = (FlutterErrorDetails details) => yDoCrash(details);
  var yThemeData = ThemeData(primarySwatch: primarySwatch ?? Colors.grey);
  yThemeData = yThemeData.copyWith(
    platform: TargetPlatform.android,
    primaryColor: YConfig.themeColor,
    colorScheme: yThemeData.colorScheme.copyWith(secondary: YConfig.themeColor),
  );
  runZonedGuarded(() async {
    runApp(MaterialApp(
      title: YConfig.appName,
      debugShowCheckedModeBanner: YConfig.DEBUG,
      theme: theme ?? yThemeData,
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        ChineseCupertinoLocalizations.delegate,
      ],
      localeResolutionCallback: (locale, supportedLocales) {
        YLocaleUtils.appLocale_system = locale!;
        yRead(YConfig.localizationKey).then((value) {
          YLocaleUtils.appLocale = value != null ? Locale(value) : locale;
          YConfig.onLocaleLoad(YLocaleUtils.appLocale!);
          for (var yPage in yPages) {
            if (yPage.mounted && yPage.yIsAlive) yPage.setState(() {});
          }
        });
        return locale;
      },
      supportedLocales: const [Locale("zh", "CH")],
      navigatorObservers: [BotToastNavigatorObserver()],
      builder: (context, child) {
        final botToastBuilder = BotToastInit();
        final yChild = botToastBuilder(context, child);
        return builder == null ? yChild : builder.call(context, yChild);
      },
      home: YStatefulWidget(() => launchPage),
    ));
  }, (Object obj, StackTrace stack) => yDoCrash(stack, obj: obj));
  yPages.add(launchPage);
}
